<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="app">
      <div>
        <div style="display: inline-block" v-for="(item,index) in arr">
          <input
            type="checkbox"
            @click="checkboxArr[index] = !checkboxArr[index]"
            v-model="checkboxArr[index]"
          />
          <span>{{item}}</span>
        </div>
        <p>你选中的元素, 累加的值和为: {{sum}}</p>
      </div>
    </div>
    <script src="../js/vue.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        data: {
          arr: [9, 15, 19, 25, 29, 31, 48, 57, 62, 79, 87],
          checkboxArr: Array.from({ length: 10 }, () => false),
        },
        // method: {
        //   check(item, index) {
        //     // 如果已经选中，点击需要
        //     // if (checkboxArr[index]) {

        //     // }else {

        //     // }

        //     checkboxArr[index] = !checkboxArr[index];
        //   },
        // },
        computed: {
          sum() {
            return this.arr.reduce((sum, item) => {
              let index = this.arr.findIndex((ele) => ele === item);
              if (this.checkboxArr[index]) {
                return sum + item;
              }else {
                return sum
              }
            }, 0);
          },
        },
      });
    </script>
  </body>
</html>
